﻿#include "Communicator.h"

Communicator::Communicator(QObject *parent)
	: QObject(parent)
{
	init();
	connect(&m_serialPort, &QSerialPort::readyRead, [this]() {
		//PtrCoreData->shiftTriggerStateByHardware();
		QByteArray recData = m_serialPort.readAll();
		qDebug() <<"接收到硬件信号：" <<recData;
		readReturnInfo(recData);
		});
	
}

Communicator::~Communicator()
{
	m_serialPort.close();

}


void Communicator::setPortInformation(HSS::PortInformation portInfo)
{
	m_portInfo = portInfo;
	setSerialPort();
}

// void Communicator::sendData()
// {
// 	QByteArray resultData;
// 	for (int i = 0;i<8;i++)
// 	{
// 		resultData.append(m_byteMsgVec[i]);
// 	}
// 
// 	m_serialPort.write(m_sendMsg);
// }

void Communicator::sendInitInfo()
{
	m_serialPort.write(m_initInfo);
}

void Communicator::sendStartInfo()
{
	m_serialPort.write(m_startInfo);
}

void Communicator::sendStopInfo()
{
	m_serialPort.write(m_stopInfo);
}

void Communicator::sendRowInfo(int frequency, int rowNum)
{
	QString frequencyStr = convertToByte(frequency);
	QString rowNumStr = convertToByte(rowNum);
	QByteArray rowInfo;
	rowInfo.append(0x5A);
	rowInfo.append(0x20);
	rowInfo.append(0x04);
	rowInfo.append(frequencyStr);
	rowInfo.append(rowNumStr);
	quint16 crcByte = calculateCRC16(rowInfo);
	rowInfo.append(crcByte);
	m_serialPort.write(rowInfo);
}

void Communicator::sendExpandInfo(int expandValue)
{
	QString expandValueStr = convertToByte(expandValue);
	QByteArray expandInfo;
	expandInfo.append(0x5A);
	expandInfo.append(0x31);
	expandInfo.append(0x01);
	expandInfo.append(expandValueStr);
	quint16 crcByte = calculateCRC16(expandInfo);
	expandInfo.append(crcByte);
	m_serialPort.write(expandInfo);

}

void Communicator::sendImgNumInfo(int imgNum)
{
	QString imgNumStr = convertToByte(imgNum);
	QByteArray imgNumInfo;
	imgNumInfo.append(0x5A);
	imgNumInfo.append(0x40);
	imgNumInfo.append(0x01);
	imgNumInfo.append(imgNumStr);
	quint16 crcByte = calculateCRC16(imgNumInfo);
	imgNumInfo.append(crcByte);
	m_serialPort.write(imgNumInfo);
}

quint16 Communicator::calculateCRC16(const QByteArray &data)
{
	quint16 crc = 0xFFFF;
	QDataStream stream(data);
	while (!stream.atEnd()) {
		quint8 byte;
		stream >> byte;
		crc ^= (quint16)byte;
		for (int i = 0; i < 8; ++i) {
			if (crc & 0x0001) {
				crc >>= 1;
				crc ^= 0xA001;
			}
			else {
				crc >>= 1;
			}
		}
	}
	return crc;
}

QString Communicator::convertToByte(int value)
{
	QString hexStr;
	hexStr = QString("%1").arg(value, 4, 16, QChar('0')).toUpper();
	return hexStr;
}

//void Communicator::openToReceiveMsg()
//{
//	isopen = true;
//	while (isopen)
//	{
//
//	}
//}

void Communicator::init()
{
	//m_byteMsgVec.resize(8);
	//m_byteMsgVec[0] = 0xAA;
	//m_byteMsgVec[1] = 0x55;
	//m_byteMsgVec[3] = 0x08;
	initIniInfo();
	initStartInfo();
	initStopInfo();

}

void Communicator::initIniInfo()
{
	m_initInfo.resize(5);
	m_initInfo[0] = 0x5A;
	m_initInfo[1] = 0x10;
	m_initInfo[2] = 0x00;
	m_initInfo[3] = 0x13;
	m_initInfo[4] = 0x5C;
}

void Communicator::initStartInfo()
{
	m_startInfo.resize(5);
	m_startInfo[0] = 0x5A;
	m_startInfo[1] = 0x8B;
	m_startInfo[2] = 0x00;
	m_startInfo[3] = 0x23;
	m_startInfo[4] = 0x37;
}

void Communicator::initStopInfo()
{
	m_stopInfo.resize(5);
	m_stopInfo[0] = 0x5A;
	m_stopInfo[1] = 0x9E;
	m_stopInfo[2] = 0x00;
	m_stopInfo[3] = 0xB3;
	m_stopInfo[4] = 0x39;
}

void Communicator::setSerialPort()
{
	m_serialPort.setPortName(m_portInfo.portName);
	m_serialPort.setBaudRate(m_portInfo.baudRate);
	m_serialPort.setDataBits(m_portInfo.dataBits);
	m_serialPort.setParity(m_portInfo.parity);
	m_serialPort.setStopBits(m_portInfo.stopBits);
	m_serialPort.setFlowControl(m_portInfo.flowCtr);
	if (m_serialPort.isOpen())
	{
		m_serialPort.close();
	}
	if (m_serialPort.open(QIODevice::ReadWrite))
	{
		qDebug() << "Serial Port Opened Successfully......";
		return;
	}
	else
	{
		qDebug() << "ERROR TO OPEN SERIAL PORT!!!";
		return;
	}
}

void Communicator::readReturnInfo(QByteArray backInfo)
{
	QByteArray targetArray;
	targetArray.append(0xA5);
	targetArray.append(0xFC);
	targetArray.append(0x01);
	if (backInfo.contains(targetArray))
	{
		qDebug() << "Communicate Error!!!";
	}
}
